<template>
  <el-dialog
    v-el-drag-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="20%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分类类型" prop="newsType" label-width="100px">
      <el-select v-model="dataForm.newsType">
        <el-option label="普通分类" :value="1"></el-option>
				<el-option label="新人教程" :value="2"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="类别Value" prop="classValue" label-width="100px">
      <el-input v-model="dataForm.classValue" placeholder="类别内容Value" style="width:200px"></el-input>
    </el-form-item>
    <el-form-item label="类别排序" prop="classSort" label-width="100px">
      <el-input type="number" v-model="dataForm.classSort" placeholder="类别排序" style="width:200px"></el-input>
    </el-form-item>
    <el-form-item label="图标" prop="img">
      <el-upload
        :class="{hide:hideUpload}"
        :action="this.$http.adornUrl(`/newInfo/uploadNewsInfo?token=${this.$cookie.get('token')}`)"
        :limit="1"
        list-type="picture-card"
        :file-list="dataForm.img"
        :on-preview="handlePreview"
        :on-success="handleSuccess"
        :on-remove="handleRemove"
        :on-exceed="onExceed">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible" :append-to-body='true'>
        <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
    </el-form-item>
    <!-- <el-form-item label="创建人" prop="createUserId">
      <el-input v-model="dataForm.createUserId" placeholder="创建人"></el-input>
    </el-form-item> -->
    <!-- <el-form-item label="创建时间" prop="createTime">
      <el-date-picker v-model="dataForm.createTime" type="datetime" value-format = "yyyy-MM-dd HH:mm:ss" placeholder="选择日期时间"></el-date-picker>
    </el-form-item> -->
    <!-- <el-form-item label="分类状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="分类状态"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        hideUpload: false,
        limitCount: 1,
        visible: false,
        dialogImageUrl: '',
        dialogVisible: false,
        dataForm: {
          classId: '',
          classKey: 0,
          classValue: '',
          classSort: '',
          createUserId: '',
          createTime: '',
          state: '',
          img: [],
          newsType: ''
        },
        dataRule: {
          newsType: [
            { required: true, message: '分类类型不能为空', trigger: 'blur' }
          ],
          classValue: [
            { required: true, message: '类别内容Value不能为空', trigger: 'blur' }
          ],
          classSort: [
            { required: true, message: '类别排序不能为空', trigger: 'blur' },
            { pattern: /^[0-9]*[1-9][0-9]*$/, message: '类别排序为正整数', trigger: 'blur' }
          ]
          // createUserId: [
          //   { required: true, message: '创建人不能为空', trigger: 'blur' }
          // ],
          // createTime: [
          //   { required: true, message: '创建时间不能为空', trigger: 'blur' }
          // ],
          // state: [
          //   { required: true, message: '分类状态不能为空', trigger: 'blur' }
          // ]
        }
      }
    },
    methods: {
      // 图片预览
      handlePreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      },
      // 文件上传成功的钩子函数
      handleSuccess (res, file, fileList) {
        this.hideUpload = fileList.length >= this.limitCount
        this.$message({
          type: 'info',
          message: '图片上传成功',
          duration: 6000
        })
        if (res.code === 0) {
          this.dataForm.img.push({name: 'tupian', url: res.data})
          console.log(this.dataForm.img)
        }
      },
      // 删除文件之前的钩子函数
      handleRemove (file, fileList) {
        this.hideUpload = fileList.length >= this.limitCount
        for (var i = 0; i < this.dataForm.img.length; i++) {
          var picc = this.dataForm.img[i]
          if (picc.url === file.url) {
            this.dataForm.img.splice(i, 1)
          }
        }
        this.$message({
          type: 'info',
          message: '已删除原有图片',
          duration: 6000
        })
      },
      // 上传的文件个数超出设定时触发的函数
      onExceed (files, fileList) {
        this.$message({
          type: 'info',
          message: '最多只能上传1个图片',
          duration: 6000
        })
      },
      init (id) {
        this.hideUpload = false
        this.dataForm.classId = id || 0
        this.visible = true
        debugger
        this.$nextTick(() => {
          debugger
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.classId) {
            this.$http({
              url: this.$http.adornUrl(`/newsClass/getNewClassById/${this.dataForm.classId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.classId = data.data.classId
                this.dataForm.classValue = data.data.classValue
                this.dataForm.classSort = data.data.classSort
                this.dataForm.createUserId = data.data.createUserId
                this.dataForm.createTime = data.data.createTime
                this.dataForm.state = data.data.state
                this.dataForm.newsType = data.data.newsType
                if (data.data.imgName !== '') {
                  this.dataForm.img.push({name: 'tu', url: 'http://image.yilianbaihui.cn/consumer/' + data.data.imgPath + '/' + data.data.imgName})
                }
              }
              this.hideUpload = true
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/newsClass/${!this.dataForm.classId ? 'addNewClass' : 'modifyNewClassById'}`),
              method: 'post',
              data: this.$http.adornData({
                'classId': this.dataForm.classId || undefined,
                'classKey': this.dataForm.classKey,
                'classValue': this.dataForm.classValue,
                'classSort': this.dataForm.classSort,
                'createUserId': this.dataForm.createUserId,
                'createTime': this.dataForm.createTime,
                'state': this.dataForm.state,
                'imgName': this.dataForm.img[0].url,
                'newsType': this.dataForm.newsType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>
  .hide .el-upload--picture-card {  display: none; }
</style>
